<?php
namespace app\index\controller;

use app\common\controller\Homebase;
use app\common\model\News;

class Index extends Homebase
{
    public function initialize()
    {
        parent::initialize();
        $this->model = new News();
    }

    public function index()
    {
        // 永久置顶文章
        $top_where = ['status'=>News::STATUS_ISSUE, 'is_top'=>News::IS_TOP_ALWAYS];
        $top_1     = $this->model->where($top_where)->limit(2)->order('id desc')->select();
        $top_1     = !empty($top_1) ? tp_object_array($top_1) : array();

        $date_time  = date('Y-m-d H:i:s');
        $top2_where[] = ['status', '=', News::STATUS_ISSUE];
        $top2_where[] = ['is_top', '=', News::IS_TOP_SPELL];
        $top2_where[] = ['top_stat_time', '>=', $date_time];
        $top2_where[] = ['top_end_time', '<=', $date_time];
        $top_2 = $this->model->where($top2_where)->limit(3)->order('id desc')->select();
        $top_2 = !empty($top_2) ? tp_object_array($top_2) : array();

        // 每日推荐文章列表
        $where = ['status'=>News::STATUS_ISSUE];
        $filed = 'a.*,b.name as column_name';
        $count = $this->model->where($where)->count('id');
        $list  = $this->model->field($filed)->alias('a')
                      ->join('__COLUMN__ b', 'a.column_id = b.id')
                      ->where(['status'=>1])
                      ->order('a.id desc')->paginate(10);

        $this->assign('list', $list);
        $this->assign('top_1', $top_1);
        $this->assign('top_2', $top_2);
        return $this->fetch();
    }
    
}
